﻿using System.Text.RegularExpressions;
using WordCloudSharp;

namespace WordCloudClient.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        private readonly IRegionManager _regionManager;
        public readonly WinEditHelper _winEditHelper;
        public readonly ISystemService _systemService;
        public DelegateCommand<object> NavigateCommand { get; private set; }
        public DelegateCommand ExecuteMouseDoubleClickDelegateCommand { get; set; }
        public DelegateCommand ExecuteOnLoadedDelegateCommand { get; private set; }
        public DelegateCommand<object> ExecuteTableItemClosedDelegateCommand { get; private set; }
        public DelegateCommand ExecuteOpenAboutSystemDelegateCommand { get; set; }
        public MainWindowViewModel(IRegionManager regionManager, WinEditHelper winEditHelper, SystemService systemService)
        {
            _regionManager = regionManager;
            _winEditHelper = winEditHelper;
            _systemService = systemService;

            NavigateCommand = new DelegateCommand<object>(Navigate);
            ExecuteMouseDoubleClickDelegateCommand = new DelegateCommand(ExecuteMouseDoubleClick);
            ExecuteOnLoadedDelegateCommand = new DelegateCommand(ExecuteOnLoaded);
            ExecuteTableItemClosedDelegateCommand = new DelegateCommand<object>(ExecuteTableItemClosed);
            ExecuteOpenAboutSystemDelegateCommand = new DelegateCommand(ExecuteOpenAboutSystem);
        }
        public void ExecuteOnLoaded()
        {
            SystemName = _systemService.GetDictionaryValueByCode("SystemName").Value;
            Navigate(0);
        }
        
        private string _systemName = string.Empty;
        public string SystemName
        {
            get { return _systemName; }
            set
            {
                _systemName = value;
                RaisePropertyChanged(nameof(SystemName));
            }
        }
        private void Navigate(object obj)
        {
            int param = Convert.ToInt32(obj);
            string? navigatePath = Enum.GetName(typeof(MenuUrlEnum), param);
            string? menuName = Enum.GetName(typeof(MenuNameEnum), param);
            if (menuName!.Contains('_'))
            {
                menuName = menuName.Replace("_", " ");
            }
            string? menuIcon = Enum.GetName(typeof(MenuIconEnum), param);
            if (!string.IsNullOrEmpty(navigatePath))
            {
                NavigationParameters parameters = new()
                {
                    { "MenuName", menuName },
                    { "MenuIcon", menuIcon }
                };
                _regionManager.RequestNavigate("ContentRegion", navigatePath, parameters);
            }
        }
        public static void ExecuteMouseDoubleClick()
        {
            if (Application.Current.MainWindow != null && Application.Current.MainWindow.Visibility != Visibility.Visible)
            {
                Application.Current.MainWindow.Show();
                WindowHelper.SetWindowToForeground(Application.Current.MainWindow);
            }
            else
            {
                if (Application.Current.MainWindow != null)
                {
                    Application.Current.MainWindow.WindowState = WindowState.Normal;
                    WindowHelper.SetWindowToForeground(Application.Current.MainWindow);
                }
            }
        }
        /// <summary>
        /// 关闭TabItem
        /// </summary>
        /// <param name="obj"></param>
        private void ExecuteTableItemClosed(object obj)
        {
            if (obj is RoutedEventArgs param)
            {
                _regionManager.Regions["ContentRegion"].Remove(param.OriginalSource);
            }
        }
        private void ExecuteOpenAboutSystem()
        {
            DialogParameters param = new()
            {
                {"Title","关于系统" }
            };
            _winEditHelper.ExecuteEidt(param, "WinAboutSystem");
        }
        

    }
}
